package com.csthink.auditlog.service;

import com.csthink.auditlog.domain.AuditLog;
import com.csthink.auditlog.domain.ScrollAuditLogs;
import com.csthink.auditlog.dto.AuditLogQueryParamDTO;
import com.csthink.auditlog.dto.PageResultDTO;
import com.csthink.auditlog.repository.AuditLogRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-12
 */
@Service
@Qualifier("webAuditLogService")
public class WebAuditLogServiceImpl extends AbstractAuditLogService implements AuditLogService{

    public WebAuditLogServiceImpl(@Qualifier("webEsAuditLogRepository") AuditLogRepository auditLogRepository) {
        this.auditLogRepository = auditLogRepository;
    }

    @Override
    public AuditLog save(AuditLog auditLog) throws IOException {
        return super.save(auditLog);
    }

    @Override
    public void asyncSave(AuditLog auditLog) {
        super.asyncSave(auditLog);
    }

    @Override
    public AuditLog findById(String id) throws IOException {
        return super.findById(id);
    }

    @Override
    public PageResultDTO<AuditLog> query(AuditLogQueryParamDTO queryParam) throws IOException {
        return super.query(queryParam);
    }

    @Override
    public ScrollAuditLogs scrollAuditLog(AuditLogQueryParamDTO queryParam) throws IOException {
        return super.scrollAuditLog(queryParam);
    }

    @Override
    public int getAuditLogCount(AuditLogQueryParamDTO queryParam) {
        List<String[]> auditLogContentList = getAuditLogContentList(queryParam);
        return auditLogContentList.size();
    }

    @Scheduled(fixedDelay = 2000) // Every 2 seconds aft
    //  last job finished.
    public void runSavingTask() {
        savingTask();
    }
}
